import { RepositoryBase } from "@repositories/repository.base";
import { ProductModel } from "@models/product.model";

class ProductRepository extends RepositoryBase<ProductModel> {
  async add(record: ProductModel): Promise<ProductModel> {
    record.id = record.createId();

    return super.add(record);
  }

  async update(newRecord: ProductModel): Promise<ProductModel> {
    const oldRecord = await this.findById(newRecord.id);
    return await super.update(oldRecord, newRecord);
  }

  async delete(record: ProductModel): Promise<void> {
    return await super.delete(record);
  }

  async getAll(): Promise<ProductModel[]> {
    return await super.find({});
  }

  async findById(id: string): Promise<ProductModel> {
    return await super.findOne({ id: id });
  }

  async findByProductId(productId: string): Promise<ProductModel> {
    return await super.findOne({ productId: productId });
  }
}

export { ProductRepository };
